<?php
/**
 *
 * Application for the function: My Preferences
 *
 * This page will display the user's preferences of cars and/or models.
 * It also enables the user to add/delete one or more entrys of their
 * preferences.
 *
 *
 * @author Chi Zhang
 * @version 1.0
 * @package dummy-auto
 *
 */

include('../config.inc');
require('dummy-app.inc');

session_start();
if ( ! (session_id() && isset($_SESSION['unum'])) ) {

        $tree = make_app("My Preferences :: Invalid User!");
        $app = $tree->app;
        $app->addChild('p', "Please Login Before Using This Function.");

        session_end();
        echo $tree->asXML();
        exit;

} elseif ($_GET['action'] == 'dci') {

        if (! mysql_query("
                delete from Car_Interested
                      where CustomerNum = $_SESSION[unum]
                        and VIN = '$_GET[vin]'
                        and Plan = $_GET[plan];"
                    )
                ) exit_404(mysql_error());

        if (! mysql_query("commit;")) exit_404(mysql_error());

} elseif ($_GET['action'] == 'dmi') {

        if (! mysql_query("
                delete from Model_Interested
                      where CustomerNum = $_SESSION[unum]
                        and CarModelNum = '$_GET[cmn]';"
                    )
                ) exit_404(mysql_error());

        if (! mysql_query("commit;")) exit_404(mysql_error());
}

$pre_car = "
        select  C.Make,
                C.Model,
                C.Style,
                C.Year,
                C.Mileage,
                C.Doors,
                C.AC,
                C.PowerWing,
                C.PowerLock,
                C.FourWD,
                C.Color,
                C.StickerPrice,
                CI.VIN,
                CI.Plan
          from  Car_Info C,
                Car_Interested CI
         where  CI.VIN = C.VIN
           and  CI.Plan = C.Plan
           and  C.CarModelNum = C.CarModelNum
           and  CI.CustomerNum = $_SESSION[unum];
";

if (! ($sql_list_1 = mysql_query($pre_car)) ) exit_404(mysql_error());
$num_rows_1 = mysql_num_rows($sql_list_1);

$pre_mod = "
        select  M.Make,
                M.Model,
                M.Style,
                MI.CarModelNum
          from  Car_Model M,
                Model_Interested MI
         where  MI.CarModelNum = M.CarModelNum
           and  MI.CustomerNum = $_SESSION[unum];
";

if (! ($sql_list_2 = mysql_query($pre_mod)) ) exit_404(mysql_error());
$num_rows_2 = mysql_num_rows($sql_list_2);


if (! $num_rows_1 && ! $num_rows_2) {
        $tree = make_app("My Preferences :: No Record");
        $app = $tree->app;
        $app->addChild('p', "My Preferences");
        $app->addChild('p', "There is no preferences found.");
} else {
        $tree = make_app("My Preferences");
        $app = $tree->app;
        $app->addChild('h2', "Car Interested:");

        $app->addChild('table');
        $app->table->addChild('tr');
        $app->table->tr->addChild('td', 'Make');
        $app->table->tr->addChild('td', 'Style');
        $app->table->tr->addChild('td', 'Year');
        $app->table->tr->addChild('td', 'Mileage');
        $app->table->tr->addChild('td', 'Feature(s)');
        $app->table->tr->addChild('td', 'Color');
        $app->table->tr->addChild('td', 'Sticker Price');

        for ($i=0; $i < $num_rows_1; $i++)
        {
                $app->table->addChild('tr');
                $row = mysql_fetch_array($sql_list_1);

                $tmp = array("$row[0] - $row[1]");
                $tmp[] = $row[2];
                $tmp[] = $row[3];
                $tmp[] = $row[4];
                $tmp[] = "$row[5] doors" .
                        ($row[6]? ", AC" : "") .
                        ($row[7]? ", PowerWing" : "") .
                        ($row[8]? ", PowerLock" : "") .
                        ($row[9]? ", 4WD" : "");
                $tmp[] = $row[10];
                $tmp[] = '$' . $row[11];
                

                for ($j=0; $j < 7; $j++)
                        $app->table->tr[$i+1]->addChild('td', $tmp[$j]);

                $app->table->tr[$i+1]->addChild('td')->addChild('input');
                $app->table->tr[$i+1]->td[$j]->input['type'] = 'button';
                $app->table->tr[$i+1]->td[$j]->input['value'] = 'delete';
                $app->table->tr[$i+1]->td[$j]->input['onclick'] =
                        "load_app('preference', 'action=dci&vin=$row[12]&plan=$row[13]')";
        }

        for ($k=0; $k < $num_rows_2; $k++)
        {
                $app->table->addChild('tr');
                $row = mysql_fetch_array($sql_list_2);

                $tmp = array("$row[0] - $row[1]");
                $tmp[] = $row[2];

                for ($j=0; $j < 2; $j++)
                        $app->table->tr[$i+$k+1]->addChild('td', $tmp[$j]);
                for ($j=2; $j < 7; $j++)
                        $app->table->tr[$i+$k+1]->addChild('td', "--");

                $app->table->tr[$i+$k+1]->addChild('td')->addChild('input');
                $app->table->tr[$i+$k+1]->td[$j]->input['type'] = 'button';
                $app->table->tr[$i+$k+1]->td[$j]->input['value'] = 'delete';
                $app->table->tr[$i+$k+1]->td[$j]->input['onclick'] =
                        "load_app('preference', 'action=dmi&cmn=$row[3]')";
        }
}

$app->addChild('br');
$app->addChild('input');
$app->input['type'] = 'button';
$app->input['value'] = 'Add a Preference';
$app->input['onclick'] = "load_app('search')";


echo $tree->asXML();
?>
